package com.anolesoft.amljmes.billing.service.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.amljmes.billing.controller.CategoryInfoController;
import com.anolesoft.amljmes.billing.dto.CategoryInfoDto;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

@Mapper
public interface CategoryInfoServiceMapper {
    @SelectProvider(type=createSQL.class, method = "queryCategorySQL")
    List<CategoryInfoDto> queryCategorySQL(CategoryInfoController.DemoVO demoVO);
    public static class createSQL {
        public static String queryCategorySQL(CategoryInfoController.DemoVO demoVO){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.id as key,a.id, a.cjr, a.cjsj, a.xgr, a.xgsj, a.factory_id, a.category_name,0 as pageNo,10 as pageSize  from jcwh_stk_category a where 1=1 ");
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getCategorynName())){
                        String categoryName = demoVO.getCategorynName();
                        sql.append(" and upper(a.category_name) = upper(#{categoryName, jdbcType=VARCHAR})");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFactoryId())){
                        String factoryId = demoVO.getCategorynName();
                        sql.append(" and upper(a.factory_id) = upper(#{factoryId, jdbcType=VARCHAR})");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    @Insert("    insert into jcwh_stk_category" +
            "      (id, cjr, factory_id, category_name)" +
            "    values" +
            "      (sys_guid(), #{cjr, jdbcType=VARCHAR}, #{factoryId, jdbcType=VARCHAR}, #{categoryName, jdbcType=VARCHAR})"
    )
    int addCategory(CategoryInfoDto inFoDtoParam);

    @Update("    update jcwh_stk_category" +
            "      set xgr = #{xgr, jdbcType=VARCHAR},xgsj = sysdate,category_name = #{categoryName, jdbcType=VARCHAR}" +
            "    where id = #{id, jdbcType=VARCHAR}"
    )
    int updateCategory(CategoryInfoDto inFoDtoParam);

    @Delete("delete from jcwh_stk_category where id = #{id, jdbcType=VARCHAR} ")
    int deleteCategory(CategoryInfoDto inFoDtoParam);

    @Select(" select count(1) from jcwh_stk_category where category_name = #{categoryName, jdbcType = VARCHAR} and factory_id = #{factoryId, jdbcType = VARCHAR} ")
    int isCategoryName(CategoryInfoDto inFoDtoParam);

    @Select("select * from jcwh_stk_category")
    List<CategoryInfoDto> CategoryAll();
}
